package com.zhouyx.userservice.controller;

import com.zhouyx.userservice.entity.User;
import com.zhouyx.userservice.service.interfaces.TestService;
import com.zhouyx.userservice.service.interfaces.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @title: UserController
 * @Author: max.zhou1
 * @Date: 2024/9/2 22:32
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/user")
@Tag(name = "示例API", description = "用于演示Swagger注解的示例API")
// 集成Knife4j的教程：https://juejin.cn/post/7392791237618040841
//@RequiredArgsConstructor
public class UserController {

//    @Autowired
    @Resource
    private UserService userService;

    @Resource
    private TestService testService;

    @GetMapping("/list")
    @Operation(summary = "user list")
    public List<User> list() {
        System.out.println("Call UserService.list");
        testService.test();
        return userService.list();
    }

    @GetMapping("/info")
    @Operation(summary = "user info")
    @Parameters({
            @Parameter(name = "id", description = "用户的ID", required = true, in = ParameterIn.QUERY)
    })
    public User getUserById(@RequestParam("id")  Long id) {
        System.out.println("Call UserService.getUserById");
        return userService.getById(id);
    }

    @PostMapping("/add")
    @Operation(summary = "user add")
    public Integer add(@RequestBody User user) {
        System.out.println("Call UserService.add");
        return userService.add(user);
    }

    @PostMapping("/update")
    @Operation(summary = "user update")
    public Integer update(@RequestBody User user) {
        System.out.println("Call UserService.update");
        return userService.update(user);
    }

    @PostMapping("/delete")
    @Operation(summary = "user delete")
    public Integer delete(Long id) {
        System.out.println("Call UserService.delete");
        return userService.deleteById(id);
    }
}

